// pages/user/user.js
import { logina, zhuce, zichan, setusername, outlogin } from "../../http/http";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: null,
    code: "",
    zichan: {},
    data: [],
    dataa: []
  },
  // 登陆
  getUserInfo(e) {
    if (e != undefined) {
      console.log(e)
      var that = this
      // 存储基本信息
      this.setData({
        user: e.detail.userInfo
      })
      // 登陆
      wx.login({
        success(res) {
          const { code } = res
          console.log(code)
          logina({ code: code }).then(res => {
            that.setData({
              data: res.data
            })
            wx.setStorageSync('token', { token: res.data.token })
            // 调用获取资产函数
            that.getzichan()
          })
        },
      })
    }

  },
  // 退出登陆
  outlogin() {
    outlogin(this.data.data.token).then(res => {
      wx.clearStorage()    // 移除指定的值
      this.setData({
        user: null
      })
      wx.redirectTo({
        url: "/pages/shouquan/shouquan"
      })
    })
  },
  // 获取用户资产
  getzichan() {
    // 获取用户资产
    zichan(this.data.data.token).then(res => {
      this.setData({
        zichan: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = wx.getStorageSync('user')
    let usera = wx.getStorageSync('usera')
    console.log(user)
    console.log(usera)
    if (user == "") {
    } else {
      this.setData({
        data: user
      })
      this.setData({
        user: usera
      })
      this.getzichan()

    }
    console.log(this.data.data)
    var that = this
    // 判断返回过来的值
    var a = typeof options.data
    if (a == "undefined") {

    } else {
      this.setData({
        user: JSON.parse(options.data)
      })
      wx.setStorageSync("usera", JSON.parse(options.data))
      // 登陆
      wx.login({
        success(res) {
          const { code } = res
          console.log(code)
          logina({ code: code }).then(res => {
            console.log(res)
            that.setData({
              data: res.data
            })
            console.log(that.data.data)
            wx.setStorageSync("user", res.data)
            wx.setStorageSync('token', { token: res.data.token })
            // 调用获取资产函数
            that.getzichan()
          })
        },
      })
    }
    var that = this
    wx.getSetting({
      success(res) {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          console.log("已经授权")
          that.getUserInfo()
        } else {
          console.log("未授权")
          wx.navigateTo({
            url: "/pages/shouquan/shouquan"
          })
        }
      },
    })
    // 判断授权状态
    var that = this
    wx.login({
      success(res) {
        const { code } = res
        that.setData({
          code: code
        })
        // 注册
        zhuce(that.data.code).then(resa => {

        })
      },
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})